package com.example.demo.config;

import com.example.demo.handler.AuthLoginIntercept;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @program: MyBatisGenerator
 * @author: daPangDong
 * @create: 2022-06-13 15:01
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    /**
     * 放行Swagger
     */
    public static final String[] SWAGGER_WHITELIST = {
            "/user/login",
            "/user/register",
            "/imserver/**",
            "/files/**",
            "/alipay/**",
            "/doc.html",
            "/webjars/**",
            "/swagger-resources/**",
            // knife4j 文档
            "/v3/api-docs/**",
            "/index/*",
    };

    @Bean
    public AuthLoginIntercept authInterceptor() {
        return new AuthLoginIntercept();
    }



    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //全局登录拦截请求
        registry.addInterceptor(authInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns(SWAGGER_WHITELIST);

    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 配置拦截器访问静态资源
        registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/favicon.ico").addResourceLocations("classpath:/META-INF/resources/");
        // knife4j 资源包
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }


}
